# -*- coding: utf-8 -*-
import scrapy

from scrapy_book.items import ScrapyBookItem


class SpiderForCSS(scrapy.Spider):
    name = 'spider_css_dangdang'

    def start_requests(self):
        for a in range(25):
            url0 = 'http://bang.dangdang.com/books/bestsellers/01.03.00.00.00.00-recent7-0-0-1-{}'.format(a+1)
            yield scrapy.Request(url=url0, callback=self.parse)

    def parse(self, response):
        items = []
        for book in response.css('body > div.bang_wrapper > div.bang_content > div.bang_list_box > ul > li'):
            item = ScrapyBookItem()
            item['title'] = book.css('div.name a::text').extract_first().replace('\n', '').strip()

            item['author'] = book.css('div:nth-child(5) a::text').extract_first().replace('\n', '').strip()

            item['pub_date'] = book.css('div:nth-child(6) span::text').extract_first().replace('\n', '').strip()

            item['publisher'] = book.css('div:nth-child(6) a::text').extract_first().replace('\n', '').strip()

            item['price'] = book.css('div.price p span.price_n::text').extract_first().replace('\n', '').strip()
            #
            # item['score'] = book.css('div.star.clearfix span.rating_nums::text').extract_first().replace('\n',
            #                                                                                              '').strip()
            # item['scrible'] = book.css('p.quote span.inq::text').extract_first().replace('\n', '').strip()
            # item['num'] = book.css('div.star.clearfix span.pl::text').extract_first().strip("(").strip(")").replace(
            #     '\n', '').strip()
            item['img'] = book.css('div.pic img').xpath('@src').extract_first().replace('\n', '').strip()
            print(item['img'])

            items.append(item)

        print(items)
        return items
